package org.wmq.shopmanager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.wmq.shopmanager.mapper.SystemUserRoleMapper;
import org.wmq.shopmanager.service.SystemRoleService;
import org.wmq.shopmanager.service.SystemUserRoleService;
import org.wmq.shopmodel.DTO.system.SystemRoleDTO;
import org.wmq.shopmodel.DTO.system.SystemUserRoleAssignDTO;
import org.wmq.shopmodel.DTO.system.SystemUserRoleQueryDTO;
import org.wmq.shopmodel.Entity.system.SystemRole;
import org.wmq.shopmodel.Entity.system.SystemUser;
import org.wmq.shopmodel.Entity.system.SystemUserRole;
import org.wmq.shopmodel.VO.system.SystemRoleVO;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
@RequiredArgsConstructor
public class SystemUserRoleServiceImpl extends ServiceImpl<SystemUserRoleMapper, SystemUserRole> implements SystemUserRoleService {
    private final SystemUserRoleMapper systemUserRoleMapper;
    private final SystemRoleService systemRoleService;

    @Override
    @Transactional
    public void assignRoleToUser(SystemUserRoleAssignDTO systemUserRoleAssignDTO) {
        Long systemUserId = systemUserRoleAssignDTO.getSystemUserId();
        this.deleteSystemUserRoleBySystemUserId(systemUserId);
        ArrayList<SystemUserRole> systemUserRoles = new ArrayList<>();
        for (Long systemRoleId :systemUserRoleAssignDTO.getSystemRoleIds()){
            SystemUserRole systemUserRole = new SystemUserRole();
            systemUserRole.setSystemUserId(systemUserId);
            systemUserRole.setSystemRoleId(systemRoleId);
            systemUserRoles.add(systemUserRole);
        }
        this.saveBatch(systemUserRoles);
    }

    @Override
    public SystemRoleVO getAllSystemRoleAndAssignedSystemRoleBySystemUserId(SystemUserRoleQueryDTO systemUserRoleQueryDTO) {
        SystemRoleDTO systemRoleDTO = new SystemRoleDTO();
        systemRoleDTO.setPageNo(systemUserRoleQueryDTO.getPageNo());
        systemRoleDTO.setPageSize(systemUserRoleQueryDTO.getPageSize());
        IPage<SystemRole> pageItem = this.systemRoleService.getPageItem(systemRoleDTO);
        Set<Long> systemRoleIds = new HashSet<>();
        for (SystemRole systemRole:pageItem.getRecords()){
            systemRoleIds.add(systemRole.getId());
        }
        List<SystemUserRole> systemUserRoles = this.getAssignedSystemUserRoleIds(systemUserRoleQueryDTO.getSystemUserId());
        List<Long> assignedRoleIds = new ArrayList<>();
        for (SystemUserRole systemUserRole:systemUserRoles){
            if (systemRoleIds.contains(systemUserRole.getSystemRoleId())){
                assignedRoleIds.add(systemUserRole.getSystemRoleId());
            }
        }
        SystemRoleVO result = new SystemRoleVO();
        result.setAssignedRoleIds(assignedRoleIds);
        result.setSystemRolePageItem(pageItem);
        return result;
    }

    private List<SystemUserRole> getAssignedSystemUserRoleIds(Long systemUserId){
        LambdaQueryWrapper<SystemUserRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(SystemUserRole::getSystemRoleId)
                .eq(SystemUserRole::getSystemUserId,systemUserId);
        return this.systemUserRoleMapper.selectList(queryWrapper);
    }

    private void deleteSystemUserRoleBySystemUserId(Long systemUserId){
        LambdaQueryWrapper<SystemUserRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SystemUserRole::getSystemUserId,systemUserId);
        this.remove(queryWrapper);
    }

}
